<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>箭头函数</title>
	<!--<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>-->
</head>
<body>
<script>
	//箭头函数是对一个函数的简写
	function f(x) {
		return x*x;
	}
	//箭头函数相当于匿名函数
	var f = x => x*x;

	//上面的箭头函数 f 简化了{} 和 return语句
	x => {
		if (x > 0) {
			return x;
		} else {
			return -x;
		}
	}
	// 不是一个参数
	(x,y) => x + y;

	//无参数
	() => 3.14;

	//可变参数
	// (x, y, ...rest) {
	// 	var sum = x + y;
	// 	for (let i; i<rest.length; i++) {
	// 		sum += sum;
	// 	}
	// 	return sum;
	// }

	//返回对象 报错
	x => {
		foo:x
	}

	x => ({
		foo:x
	})

	// var obj = {
	// 	birth:1990,
	// 	getAge:function () {
	// 		var b = this.birth;
	// 		var fn = function () {
	// 			return new Date().getFullYear() - this.birth;
	// 		};
	// 		//此时的this指向window
	// 		return fn();
	// 	}
	// }
	// console.log("getAge():" + obj.getAge());

	var obj = {
		birth:1992,
		getAge:function (year) {
			let b = this.birth;
			//其中的this已经按照词法作用域进行了绑定。绑定到了birth
			let fn = y => y - this.birth;
			return fn.call({birth:2000},year);
		}
	}
	console.log("obj.getAge(2015):" + obj.getAge(2015));

	let obj1 = {
		birth:1990,
		getAge:function (year) {
			let b = this.birth;
			let fn = (y) => new Date().getFullYear() - year;
			return fn.call({birth:1998},year);
		}
	};
	console.log("obj.getAge():" + obj1.getAge(2004));
</script>
</body>
</html>